﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SantaSLittleIrcHelper.Registration;

namespace SantaSLittleIrcHelper
{

    public enum Consoles { feedback, sent, received };


    public partial class GUI : Form
    {

        private delegate void SetTextCallback(string text);

        public GUI()
        {
            InitializeComponent();
        }

        public T getElement<T>(String what)
        {
            Object[] x = this.Controls.Find(what, true);
            if (x.Length == 0) throw new Exception("GUI Element " + what + " not found!");
            return (T)x[0];
        }

        public void updateTextBoxes()
        {
            Settings s = Program.instance.settings;
            this.username.Text = s.nsUsername;
            this.password.Text = s.nsPassword;
            this.email.Text = s.nsEmail;
            this.confirmation.Text = s.nsConfirmation;
        }

        public void addConsoleMessage(String s, Consoles c)
        {
            TextBox console;
            switch (c)
            {
                case Consoles.feedback:
                    console = this.cFeedback;
                    break;
                case Consoles.received:
                    console = this.cReceived;
                    break;
                case Consoles.sent:
                    console = this.cSent;
                    break;
                default:
                    throw new Exception("Supplied undefined console");
            }
            SetTextCallback d = new SetTextCallback(console.AppendText);
            console.Invoke(d, new object[] { s + "\r\n" });
        }

        public void invokeChangeStatus(System.Drawing.Color color, String text)
        {
            Label l = this.labelConnected;
            l.ForeColor = color;
            l.Text = text;
            this.Refresh();
        }

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            if (Program.instance.IRC.IsConnected)
            {
                Program.instance.consoleWrite(Languages.Instance.get("errorAlreadyConnected"));
                return;
            }
            Program.instance.connect();
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            Program.instance.IRC.WriteLine(this.chatLineBox.Text);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Program.instance.IRC != null && Program.instance.IRC.IsConnected == true)
            {
                Program.instance.disconnect();
                e.Cancel = true;
            }
            Program.instance.terminate();

        }

        private void buttonDisconnect_Click(object sender, EventArgs e)
        {
            if (!Program.instance.IRC.IsConnected)
            {
                Program.instance.consoleWrite(Languages.Instance.get("errorNotConnectedYet"));
                return;
            }
            Program.instance.disconnect();
        }

        private void buttonAvailability_Click(object sender, EventArgs e)
        {
            Program.instance.startProcess(new CheckAvailabilityProcess());
        }

        private void chatLineBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) buttonSend_Click(sender, e);

        }

        private void buttonRegister_Click(object sender, EventArgs e)
        {
            Program.instance.startProcess(new RegistrationProcess());
        }

        private void buttonConfirm_Click(object sender, EventArgs e)
        {
            Program.instance.startProcess(new ConfirmationProcess());
        }

        private void username_TextChanged(object sender, EventArgs e)
        {
            Program.instance.settings.nsUsername = this.username.Text;
        }

        private void password_TextChanged(object sender, EventArgs e)
        {
            Program.instance.settings.nsPassword = this.password.Text;
        }

        private void email_TextChanged(object sender, EventArgs e)
        {
            Program.instance.settings.nsEmail = this.email.Text;
        }

        private void confirmation_TextChanged(object sender, EventArgs e)
        {
            Program.instance.settings.nsConfirmation = this.confirmation.Text;
        }

        private void GUI_Load(object sender, EventArgs e)
        {

            Program.instance.GUI.updateTextBoxes();

            ToolStripMenuItem[] items = new ToolStripMenuItem[Languages.Instance.languages.Count];
            int i = 0;
            foreach (KeyValuePair<String, String> kvp in Languages.Instance.languages)
            {
                ToolStripMenuItem temp = new ToolStripMenuItem();
                temp.Text = kvp.Value;
                temp.Name = kvp.Key;
                if (kvp.Key == Program.instance.settings.currentLanguage)
                {
                    temp.Checked = true;
                }
                temp.Click += new System.EventHandler(this.languageChange_Click);
                items[i] = temp;
                i++;
            }
            this.languageToolStripMenuItem.DropDownItems.AddRange(items);

            if (Program.instance.settings.debugMode) this.debugModeToolStripMenuItem.Checked = true;
            if (Program.instance.settings.verboseMode) this.verboseModeToolStripMenuItem.Checked = true;

            Languages.Instance.translateAll();
        }

        private void debugElementsVisible()
        {
            if (Program.instance.settings.debugMode)
            {
                if (!this.tabControl1.Controls.Contains(this.tabPage4)) this.tabControl1.Controls.Add(this.tabPage4);
            }
            else
            {
                this.tabControl1.Controls.Remove(this.tabPage4);
            }
        }

        private void languageChange_Click(object sender, EventArgs e)
        {
            if (((ToolStripMenuItem)sender).Name == Program.instance.settings.currentLanguage) return;
            ((ToolStripMenuItem)this.languageToolStripMenuItem.DropDownItems.Find(Program.instance.settings.currentLanguage, true)[0]).Checked = false;
            Languages.Instance.changeLanguage(((ToolStripMenuItem)sender).Name);
            ((ToolStripMenuItem)this.languageToolStripMenuItem.DropDownItems.Find(Program.instance.settings.currentLanguage, true)[0]).Checked = true;
        }

        private void debugModeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.debugModeToolStripMenuItem.Checked = !this.debugModeToolStripMenuItem.Checked;
            Program.instance.settings.debugMode = this.debugModeToolStripMenuItem.Checked;
            this.debugElementsVisible();
        }

        private void verboseModeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.verboseModeToolStripMenuItem.Checked = !this.verboseModeToolStripMenuItem.Checked;
            Program.instance.settings.verboseMode = this.verboseModeToolStripMenuItem.Checked;
        }
    }
}
